package org.mparts.client;

import org.mparts.client.ui.IMenuView;
import org.mparts.client.ui.MpartsCenterContainer;
import org.mparts.client.ui.IMenuView.IMenuPresenter;
import org.mparts.client.ui.INewsView.INewsPresenter;
import org.mparts.client.ui.events.ShowNewsEvent;
import org.mparts.client.ui.events.ShowNewsEventHandler;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import com.sencha.gxt.core.client.Style.LayoutRegion;
import com.sencha.gxt.widget.core.client.Composite;
import com.sencha.gxt.widget.core.client.ContentPanel;
import com.sencha.gxt.widget.core.client.container.BorderLayoutContainer;
import com.sencha.gxt.widget.core.client.container.SimpleContainer;

public class MpartsShell extends Composite {

	public interface Binder extends UiBinder<Widget, MpartsShell> {
	}
	
	@UiField
	MpartsCenterContainer centerPanel;
	
	@UiField
	SimpleContainer northPanel;
	
	@UiField
	ContentPanel eastPanel;
	
	@UiField
	BorderLayoutContainer cont;
	
	private IMenuView menuView;

	public MpartsShell() {
		Binder uiBinder = GWT.create(Binder.class);
		initWidget(uiBinder.createAndBindUi(this));
//		centerPanel = new MpartsCenterContainer(this);
	}
		
	public void MpartsShell1() {
		monitorWindowResize = true;
		Window.enableScrolling(false);
		setPixelSize(Window.getClientWidth(), Window.getClientHeight());

		setStateful(true);
		setStateId("mpartsLayout");

//		BorderLayoutStateHandler state = new BorderLayoutStateHandler(this);
//		state.loadState();

//		setCenterWidget(centerPanel, new BorderLayoutData());
	}

	@Inject
	public MpartsShell(IMenuPresenter menuPresenter, INewsPresenter newsPresenter, EventBus eventBus) {
		this();
		Log.debug("create injected MpartsShell");
		this.menuView = menuPresenter.getView();
		northPanel.add(menuView);
		eastPanel.add(newsPresenter.getView());
//		BorderLayoutData northData = new BorderLayoutData(140);
//		northData.setMargins(new Margins(0, 5, 0, 0));
//		setNorthWidget(menuView, northData);
//		BorderLayoutData layoutData = new BorderLayoutData(200);
//		layoutData.setMargins(new Margins(0, 0, 0, 2));
//		VerticalLayoutContainer eastCont = new VerticalLayoutContainer();
//		eastCont.add(newsView, new VerticalLayoutData(1, -1));
//		eastCont.addStyleName("bgBlack");
//		setEastWidget(newsPresenter.getView(), layoutData);
		
		eventBus.addHandler(ShowNewsEvent.TYPE, getShowNewsHandler());
	}

	public AcceptsOneWidget getDisplay() {
		return centerPanel;
	}
	
	
	public void forceLayout() {
		cont.forceLayout();
	}
	
	private ShowNewsEventHandler getShowNewsHandler() {
		return new ShowNewsEventHandler() {
			
			@Override
			public void onShowNews(ShowNewsEvent showNewsEvent) {
				if ( showNewsEvent != null ) {
					if ( showNewsEvent.isShow() ) {
						Log.debug("expand east region");
						cont.expand(LayoutRegion.EAST);
					} else {
						Log.debug("collapse east region");
						cont.collapse(LayoutRegion.EAST);
					}
				}
			}
		};
	}
	
//	@Override
//	public void onResize() {
//		super.onResize();
//		doLayout();
//	}
//
//	@Override
//	protected void onWindowResize(int width, int height) {
//		setPixelSize(width, height);
//		forceLayout();
//	}
	
}
